home *** CD-ROM | disk | FTP | other *** search
- /* qsort on page 319 of the Turbo C Bible */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int mycompare(const void *, const void *);
- main(int argc, char **argv, char **envp)
- {
- unsigned int i, count;
- char **p_table, **result;
- /* Find length of encironment table and print it */
- printf("==== Unsorted environment table ====\n");
- for(count = 0, p_table = envp; *p_table != NULL; p_table++, count++)
- printf("%s\n", *p_table);
- /* sort the environment table using "qsort" */
- qsotr((void *) envp, (size_t)count, (size_t)sizeof(char *), mycompare);
- /* Print sorted environment table */
- printf("===== Sorted environment table =====\n");
- for(i = 0, p_table = envp; i< count; i++)
- {
- printf("%s\n", *p_table);
- p_table++;
- }
- }
- /* ----------------------------- */
- int mycompare(const void *arg1, const void *arg2)
- {
- /* Compare two strings up to the length of the key */
- return(strncmp(*(char**)arg1, *(char**)arg2, strlen(*(char**)arg1)));
- }